﻿using System.Net.Http.Json;
using Quartz;

namespace Digitalmes.Infrastructure.Jobs.MyJob;

/// <summary>
/// JOB 示例。
/// </summary>
//[JobSchedule("Example", "0 0 12 * * ?")]
public sealed class ExampleJob(IHttpClientFactory httpClientFactory, ILogger<ExampleJob> logger) : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        if (context.CancellationToken.IsCancellationRequested)
        {
            return;
        }

        await Task.Delay(1000).ConfigureAwait(false);

        var jobKey = context.JobDetail.Key;
        logger.LogInformation("Job '{JobKey}' 开始执行", jobKey);

        try
        {
            var httpclient = httpClientFactory.CreateClient(JobConstant.LocalHttpClient);

            // do something ...
            await httpclient.PostAsJsonAsync("http://localhost:5001/api/demo", new { name = "" }, context.CancellationToken);

            logger.LogInformation("Job '{JobKey}' 执行结束", jobKey);
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Job '{JobKey}' 执行异常", jobKey);
        }
    }
}
